<?php
namespace PsiFramework\Applications\Admin\Classes\Actions\CMS;

import("Classes.Actions.AbstractAction");
import("Classes.Factory.DAOFactory");
import("Classes.Factory.ObjectFactory");

use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Factory\ObjectFactory;
use \PsiFramework\Classes\Util\Database\Database;
use \PsiFramework\Classes\Util\Manager\ApplicationManager;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action de sauvegarde
 *
 * @package Actions
 */
class SaveAction extends AbstractAction
{

    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     */
    public function execute()
    {
        parent::execute();

        $database = Database::getInstance();
        $database->startTransaction();

        try {
            $obj = null ;
            $dao = DAOFactory::getDAO($this->_objectType);
            if ($this->_id != null) {
                $obj = $dao->retrieveByPk($this->_id);
            }

            if ($obj == null) {
                $obj = ObjectFactory::getNewObject($this->_objectType);
            }

            switch(mb_strtoupper($this->_objectType, 'utf-8')) {
            case "USER" :

                $obj->setId($this->_request->getParameter('id'));
                $obj->setLogin($this->_request->getParameter('login'));

                if (isNullOrEmptyString($obj->getId())) {
                    $obj->setCreationDate(Date::now());
                }

                $pwd = $this->_request->getParameter('old_password');
                $decryptedPwd = $this->_request->getParameter('password');
                if ($pwd != $decryptedPwd) {
                    $pwd = md5($decryptedPwd);
                    $obj->setPassword($pwd);
                }

                $email = $this->_request->getParameter('email');
                $obj->setEmail($email);

                $obj->setActivated(true);
                $obj->setBlocked(false);

                $dao->save($obj, false);

                $securityRoleDao = DAOFactory::getDAO("SecurityRole");
                $securityRole
                    = $securityRoleDao->getRoleByName('allomedia_admin');

                $userRoleDao = DAOFactory::getDAO("SecurityUserRole");
                $userRoleModel
                    = ObjectFactory::getNewObject("SecurityUserRole");
                $userRoleModel->setUser($obj);
                $userRoleModel->setRole($securityRole);

                $userRoleDao->save($userRoleModel);

                break;
            }

            if (!is_null($obj)) {
                $dao->save($obj, false);
            }

            $database->commit();
        }catch(\Exception $e) {
            $database->rollback();
            throw $e ;
        }

        $objectParameters
            = ApplicationManager::getInstance()->
                                getApplication()->
                                getObjectParameter($this->_objectType);

        $this->_response->setAttribute("object", $obj);
        $this->_response->setAttribute(
            "columns",
            $objectParameters->getParameters()
        );
        $this->findForward('success');
    }
}
